/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
// client.h -- primary header for client

//define	PARANOID			// speed sapping error checking

#include "ref.h"
#include "vid.h"
#include "screen.h"
#include "sound.h"
#include "input.h"
#include "keys.h"
#ifdef CD_AUDIO
#include "cdaudio.h"
#endif

#ifdef USE_CURL
#define CURL_STATICLIB
#include <curl/curl.h>
#endif

//=============================================================================

#define	MAX_PARSE_ENTITIES		1024
#define PARSE_ENTITIES_MASK		( MAX_PARSE_ENTITIES - 1 )

struct frame_t
{
	qboolean		valid;			// cleared if delta parsing was invalid
	int				serverframe;
	int				servertime;		// server time the message is valid for (in msec)
	int				deltaframe;
	byte			areabits[MAX_MAP_AREAS/8];		// portalarea visibility bits
	player_state_t	playerstate;
	int				numEntities;
	int				parseEntitiesIndex;	// non-masked index into cl.parseEntities array
};

struct centity_t
{
	entity_state_t	baseline;		// delta from this if not from a previous frame
	entity_state_t	current;
	entity_state_t	prev;			// will always be valid, but might just be a copy of current

	int			serverframe;		// if not current, this ent isn't in the frame

	int			trailcount;			// for diminishing grenade trails
	vec3_t		lerpOrigin;		// for trails (variable hz)

	float				flashStartTime;	// Muzzle flash effect start time in seconds
	float				flashRotation;	// Muzzle flash effect rotation

	int			fly_stoptime;
};

#define MAX_CLIENTWEAPONMODELS		20		// PGM -- upped from 16 to fit the chainfist vwep
typedef struct {
	qboolean			valid;
	char				name[MAX_QPATH];
	char				info[MAX_QPATH];
	struct model_t		*model;
	struct material_t	*skin;
	struct material_t	*icon;
	struct model_t		*weaponModel[MAX_CLIENTWEAPONMODELS];
} clientInfo_t;

#if 0
struct clientInfo_t
{
	char	name[MAX_QPATH];
	char	cinfo[MAX_QPATH];
	struct image_t	*skin;
	struct image_t	*icon;
	char	iconname[MAX_QPATH];
	struct model_t	*model;
	struct model_t	*weaponmodel[MAX_CLIENTWEAPONMODELS];
};
#endif
//extern char cl_weaponmodels[MAX_CLIENTWEAPONMODELS][MAX_QPATH];
//extern int num_cl_weaponmodels;

#define	CMD_BACKUP		64	// allow a lot of command backups for very fast systems
#define CMD_MASK		(CMD_BACKUP-1)

#ifdef USE_CURL
void CL_CancelHTTPDownloads (qboolean permKill);
void CL_InitHTTPDownloads (void);
qboolean CL_QueueHTTPDownload (const char *quakePath);
void CL_RunHTTPDownloads (void);
qboolean CL_PendingHTTPDownloads (void);
void CL_SetHTTPServer (const char *URL);
void CL_HTTP_Cleanup (qboolean fullShutdown);

#define NUM_CROSSHAIRS			20

enum dlq_state
{
	DLQ_STATE_NOT_STARTED,
	DLQ_STATE_RUNNING,
	DLQ_STATE_DONE
};

struct dlqueue_t
{
	dlqueue_t			*next;
	char				quakePath[MAX_QPATH];
	dlq_state			state;
};

struct dlhandle_t
{
	CURL		*curl;
	char		filePath[MAX_OSPATH];
	FILE		*file;
	dlqueue_t	*queueEntry;
	size_t		fileSize;
	size_t		position;
	double		speed;
	char		URL[576];
	char		*tempBuffer;
};
#endif

struct client_history_t
{
	int		realtime;	// time sent, for calculating pings
	int		cmdNumber;	// current cmdNumber for this frame
};

struct gameMedia_t
{
	// Sounds
	struct sfx_t		*sfxRichotecs[3];
	struct sfx_t		*sfxSparks[3];
	struct sfx_t		*sfxFootSteps[4];
	struct sfx_t		*sfxLaserHit;
	struct sfx_t		*sfxRailgun;
	struct sfx_t		*sfxRocketExplosion;
	struct sfx_t		*sfxGrenadeExplosion;
	struct sfx_t		*sfxWaterExplosion;
	struct sfx_t		*sfxLightning;
	struct sfx_t		*sfxDisruptorExplosion;

	// Models
	struct model_t		*modParasiteBeam;
	struct model_t		*modPowerScreenShell;
	struct model_t		*modMachinegunBrass;
	struct model_t		*modShotgunBrass;

	// Materials
	struct material_t	*levelshot;
	struct material_t	*levelshotDetail;
	struct material_t	*loadingLogo;
	struct material_t	*loadingDetail[2];
	struct material_t	*loadingPercent[20];
	struct material_t	*lagometerMaterial;
	struct material_t	*disconnectedMaterial;
	struct material_t	*pauseMaterial;
	struct material_t	*crosshairMaterials[NUM_CROSSHAIRS];
	struct material_t	*hudNumberMaterials[2][11];
	struct material_t	*fireScreenMaterial;
	struct material_t	*waterBlurMaterial;
	struct material_t	*doubleVisionMaterial;
	struct material_t	*underWaterVisionMaterial;
	struct material_t	*irGogglesMaterial;
	struct material_t	*rocketExplosionMaterial;
	struct material_t	*rocketExplosionWaterMaterial;
	struct material_t	*grenadeExplosionMaterial;
	struct material_t	*grenadeExplosionWaterMaterial;
	struct material_t	*bfgExplosionMaterial;
	struct material_t	*bfgBallMaterial;
	struct material_t	*plasmaBallMaterial;
	struct material_t	*waterPlumeMaterial;
	struct material_t	*waterSprayMaterial;
	struct material_t	*waterWakeMaterial;
	struct material_t	*nukeShockwaveMaterial;
	struct material_t	*bloodBlendMaterial;
	struct material_t	*bloodSplatMaterial[2];
	struct material_t	*bloodCloudMaterial[2];
	struct material_t	*powerScreenShellMaterial;
	struct material_t	*invulnerabilityShellMaterial;
	struct material_t	*quadDamageShellMaterial;
	struct material_t	*doubleDamageShellMaterial;
	struct material_t	*halfDamageShellMaterial;
	struct material_t	*genericShellMaterial;
	struct material_t	*laserBeamMaterial;
	struct material_t	*grappleBeamMaterial;
	struct material_t	*lightningBeamMaterial;
	struct material_t	*heatBeamMaterial;

	struct material_t	*glowParticleMaterial;


	struct material_t	*liteSmokeParticleMaterial;

	struct material_t	*dropletParticleMaterial;
	struct material_t	*steamParticleMaterial;
	struct material_t	*sparkParticleMaterial;
	struct material_t	*impactSparkParticleMaterial;
	struct material_t	*trackerParticleMaterial;
	struct material_t	*flyParticleMaterial;

	struct material_t	*bulletMarkMaterial;
	struct material_t	*burnMarkMaterial;
	struct material_t	*bloodMarkMaterials[2][6];

	// Particles
	struct particleSource_t	*blasterTrailParticle;
	struct particleSource_t	*grenadeTrailParticle;
	struct particleSource_t	*rocketTrailParticle;
	struct particleSource_t	*rocketTrailParticle2;
	struct particleSource_t	*railTrailParticle;
	struct particleSource_t	*railTrailParticle2;
	struct particleSource_t	*bubbleTrailParticle;
	struct particleSource_t	*blasterParticle;
	struct particleSource_t	*blasterParticle2;
	struct particleSource_t	*bulletParticle;
	struct particleSource_t	*bulletParticle2;
	struct particleSource_t	*tracerParticle;
	struct particleSource_t	*explotionParticle;
	struct particleSource_t	*explotionParticle2;
	struct particleSource_t	*smokePuffParticle;
	struct particleSource_t	*bubbleParticle;
	struct particleSource_t	*sparkParticle;
	struct particleSource_t	*sparkParticle2;
	struct particleSource_t	*damageSparkParticle;

	// Files referenced by the server that the client needs
	struct sfx_t		*gameSounds[MAX_SOUNDS];
	struct model_t		*gameModels[MAX_MODELS2];
	struct cmodel_t		*gameCModels[MAX_MODELS2];
	struct material_t	*gameMaterials[MAX_IMAGES];
};

//
// the client_state_t structure is wiped completely at every
// server map change
//
struct client_state_t
{
	unsigned int timeoutcount;

	unsigned int timedemo_frames;
	unsigned int timedemo_start;

	qboolean	refresh_prepped;	// false if on new level or new ref dll
	qboolean	sound_prepped;		// ambient sounds can start
	qboolean	force_refdef;		// vid has changed, so we can't use a paused refdef

	entity_state_t		parseEntities[MAX_PARSE_ENTITIES];
	int			parse_entities;		// index (not anded off) into cl.parseEntities[]

	centity_t			entities[MAX_EDICTS];

	qboolean	sendPacketNow;
	usercmd_t	cmd;
	usercmd_t	cmds[CMD_BACKUP];	// each mesage will send several old cmds
	int			cmdNumber;
	int16		predicted_origins[CMD_BACKUP][3];	// for debug comparing against server
	client_history_t	history[CMD_BACKUP];

	float		predicted_step;				// for stair up smoothing
	unsigned int predicted_step_time;
	int			predicted_step_frame;
	vec3_t		move;

	vec3_t		predicted_origin;	// generated by CL_PredictMovement
	vec3_t		predicted_angles;
	vec3_t		predicted_velocity;
	vec3_t		prediction_error;

	frame_t		frame;				// received from server
	int			surpressCount;		// number of messages rate supressed
	frame_t		frames[UPDATE_BACKUP];

	int			serverTime;

	// the client maintains its own idea of view angles, which are
	// sent to the server each frame.  It is cleared to 0 upon entering each level.
	// the server sends a delta each frame which is added to the locally
	// tracked view angles to account for standing on rotating objects,
	// and teleport direction changes
	vec3_t		viewangles;

	int			time;			// this is the time value that the client
								// is rendering at.  always <= cls2.realtime
	//int			initial_server_frame;
	float		lerpFrac;		// between oldframe and frame

	player_state_t		*playerState;
	player_state_t		*oldPlayerState;

	renderView_t	renderView;
	vec3_t				renderViewAngles;

	vec3_t		v_forward, v_right, v_up;	// set when refdef.angles is set

	//
	// transient data from server
	//
	char		layout[1024];		// general 2D overlay
	int			inventory[MAX_ITEMS];

	//
	// non-gameserver infornamtion
	// FIXME: move this cinematic stuff into the cin_t structure
	fileHandle_t cinematic_file;
	int			cinematictime;		// cls2.realtime for first cinematic frame
	int			cinematicframe;
	unsigned char		cinematicpalette[768];
	qboolean	cinematicpalette_active;

	//
	// server state information
	//
	qboolean	attractloop;		// running the attract loop, any key will menu
	int			servercount;	// server identification for prespawns
	char		gamedir[MAX_QPATH];
	int			playernum;
	int			maxclients;
	pmoveParams_t pmp;

	char		configStrings[MAX_CONFIGSTRINGS][MAX_QPATH];

	//
	// locally derived information from server state
	//
	//struct model_t	*model_draw[MAX_MODELS2];
	//struct cmodel_t	*model_clip[MAX_MODELS2];

	//struct sfx_t	*sound_precache[MAX_SOUNDS];
	//struct image_t	*image_precache[MAX_IMAGES];

	clientInfo_t		clientInfo[MAX_CLIENTS];
	clientInfo_t		baseClientInfo;

	char				weaponModels[MAX_CLIENTWEAPONMODELS][MAX_PATH_LENGTH];
	int					numWeaponModels;

	byte			demoFrame[1400];
	sizebuf_t		demoBuff;

	unsigned		settings[SVSET_MAX];
	
	// Crosshair names
	int					crosshairEntTime;
	int					crosshairEntNumber;

	gameMedia_t media;
};

extern	client_state_t	cl;

/*
==================================================================

the clientStatic2_t structure is persistant through an arbitrary number
of server connections

==================================================================
*/
struct playerStatus_t
{
	char name[16];
	int ping;
	int score;
};

#define MAX_PLAYERSTATUS	32

struct serverStatus_t
{
	char	address[MAX_QPATH];
	char	infostring[MAX_INFO_STRING];
	playerStatus_t	players[MAX_PLAYERSTATUS];
	int	numPlayers;
};

 enum connstate_t
 {
	ca_uninitialized,
	ca_disconnected, 	// not talking to a server
	ca_connecting,		// sending request packets to the server
	ca_connected,		// netchan_t established, waiting for svc_serverdata
	ca_active			// game views should be displayed
};

/*typedef enum {
	dl_none,
	dl_model,
	dl_sound,
	dl_skin,
	dl_single
} dltype_t;		// download type
*/
enum keydest_t
{
	key_game, key_console, key_message, key_menu
};

#define FPS_FRAMES						64

// The client precaches these files during initialization
struct media2_t
{
	struct material_t	*cinematicMaterial;
	struct material_t	*whiteMaterial;
	struct material_t	*consoleMaterial;
	struct material_t	*charsetMaterial;

	fontSet_t			*genericFont;
};

struct clientStatic2_t
{
	connstate_t	state;
	keydest_t	key_dest;

	//int			framecount;
	unsigned int realtime;			// always increasing, no clamping, etc
	float		 frametime;			// seconds since last frame

	glConfig_t			glConfig;

	media2_t				media;				// Precache

	int						fpsIndex;
	int						fpsTimes[FPS_FRAMES];

// screen rendering information
	unsigned int disable_screen;		// showing loading plaque between levels
									// or changing rendering dlls
									// if time gets > 30 seconds ahead, break it
	int			disable_servercount;	// when we receive a frame and cl.servercount
									// > cls2.disable_servercount, clear disable_screen

// connection information
	char		servername[MAX_OSPATH];	// name of server from original connect
	char		lastservername[MAX_OSPATH];	// name of server from original connect
	int			connect_time;		// for connection retransmits

	int			quakePort;			// a 16 bit value that allows quake servers
									// to work around address translating routers
	netchan_t	netchan;
	int			serverProtocol;		// in case we are doing some kind of version hack

	int			challenge;			// from the server to use for connecting

	FILE		*download;			// file transfer from server
	char		downloadtempname[MAX_OSPATH];
	char		downloadname[MAX_OSPATH];
	//int		downloadnumber;
	//dltype_t	downloadtype;
	int			downloadpercent;

	qboolean	failed_download;
	size_t		downloadposition;

// demo recording info must be here, so it isn't cleared on level change
	qboolean	demorecording;
	qboolean	demowaiting;	// don't record until a non-delta message is received
	fileHandle_t demofile;

	qboolean		demoplaying;
	unsigned int	timeDemoFrames;		// counter of rendered frames
	unsigned int	timeDemoStart;		// cls2.realtime before first frame
	unsigned int	timeDemoBaseTime;	// each frame will be at this time + frameNum * 50

	int doscreenshot;

	char mapname[MAX_QPATH];

	unsigned int lastSpamTime; //last client reply time
	unsigned int spamTime;
	unsigned int roundtime; //Hack to show roundtime in aq2 mod

	int			protocolVersion;	// R1Q2 protocol minor version

#ifdef USE_CURL
	dlqueue_t		downloadQueue;			//queue of paths we need
	
	dlhandle_t		HTTPHandles[4];			//actual download handles
	//don't raise this!
	//i use a hardcoded maximum of 4 simultaneous connections to avoid
	//overloading the server. i'm all too familiar with assholes who set
	//their IE or Firefox max connections to 16 and rape my Apache processes
	//every time they load a page... i'd rather not have my q2 client also
	//have the ability to do so - especially since we're possibly downloading
	//large files.

	char			downloadServer[512];	//base url prefix to download from
	char			downloadReferer[32];	//libcurl requires a static string :(
#endif
};

extern clientStatic2_t	cls2;

//=============================================================================

//
// cvars
//

extern	cvarOLD_t	*cl_stereo_separation;
extern	cvarOLD_t	*cl_stereo;

extern	cvarOLD_t	*cl_gun;
extern	cvarOLD_t	*cl_add_blend;
extern	cvarOLD_t	*cl_add_lights;
extern	cvarOLD_t	*cl_add_particles;
extern	cvarOLD_t	*cl_add_entities;
//extern	cvarOLD_t	*cl_predict;
extern	cvarOLD_t	*cl_footsteps;
extern	cvarOLD_t	*cl_noskins;
//extern	cvarOLD_t	*cl_autoskins;

extern	cvarOLD_t	*cl_upspeed;
extern	cvarOLD_t	*cl_forwardspeed;
extern	cvarOLD_t	*cl_sidespeed;

extern	cvarOLD_t	*cl_yawspeed;
extern	cvarOLD_t	*cl_pitchspeed;

extern	cvarOLD_t	*cl_run;

extern	cvarOLD_t	*cl_anglespeedkey;

extern	cvarOLD_t	*cl_shownet;
extern	cvarOLD_t	*cl_showmiss;
extern	cvarOLD_t	*cl_showclamp;

extern	cvarOLD_t	*lookspring;
extern	cvarOLD_t	*lookstrafe;
extern	cvarOLD_t	*sensitivity;

extern	cvarOLD_t	*m_pitch;
extern	cvarOLD_t	*m_yaw;
extern	cvarOLD_t	*m_forward;
extern	cvarOLD_t	*m_side;

extern	cvarOLD_t	*freelook;

extern	cvarOLD_t	*cl_lightlevel;	// FIXME HACK

extern	cvarOLD_t	*cl_paused;
extern	cvarOLD_t	*cl_timedemo;

extern	cvarOLD_t	*cl_vwep;

extern	cvarOLD_t	*ch_alpha;
extern	cvarOLD_t	*ch_pulse;
extern	cvarOLD_t	*ch_scale;
extern	cvarOLD_t	*ch_red;
extern	cvarOLD_t	*ch_green;
extern	cvarOLD_t	*ch_blue;
extern	cvarOLD_t	*con_notifylines;
extern	cvarOLD_t	*con_notifyfade;
extern	cvarOLD_t	*con_alpha;
extern	cvarOLD_t	*con_scrlines;
extern	cvarOLD_t	*cl_clock;
extern	cvarOLD_t	*cl_fps;
extern	cvarOLD_t	*cl_chathud;
extern	cvarOLD_t	*cl_chathudlines;
extern	cvarOLD_t	*cl_maptime;
extern	cvarOLD_t	*cl_timestamps;
extern	cvarOLD_t	*cl_timestampsformat;
extern	cvarOLD_t	*scr_conheight;
extern	cvarOLD_t	*cl_hudalpha;
extern	cvarOLD_t	*cl_textcolors;
extern	cvarOLD_t	*cl_autorecord;

#ifdef USE_CURL
extern	cvarOLD_t	*cl_http_downloads;
extern	cvarOLD_t	*cl_http_filelists;
extern	cvarOLD_t	*cl_http_proxy;
extern	cvarOLD_t	*cl_http_max_connections;
#endif

struct cdlight_t
{
	int		key;				// so entities can reuse same entry
	vec3_t	color;
	vec3_t	origin;
	float	radius;
	float	die;				// stop lighting after this time
	//float	decay;				// drop this each second
	//float	minlight;			// don't add when contributing less
};

//extern	centity_t	cl_entities[MAX_EDICTS];
//extern	cdlight_t	cl_dlights[MAX_DLIGHTS];

// the cl.parseEntities must be large enough to hold UPDATE_BACKUP frames of
// entities, so that when a delta compressed message arives from the server
// it can be un-deltad from the original 
//#define	MAX_PARSE_ENTITIES		1024
//#define PARSE_ENTITIES_MASK		( MAX_PARSE_ENTITIES - 1 )
//extern	entity_state_t	cl_parse_entities[MAX_PARSE_ENTITIES];

//=============================================================================

extern	netadr_t	net_from;
extern	sizebuf_t	net_message;

#ifdef GL_QUAKE
void Draw_String (int x, int y, const char *s, int color, float alpha, qboolean alt);
void DrawCString (int x, int y, const short *s, float alpha, int enable);

//#define DrawString(x, y, s)				Draw_String((x), (y), (s), 7, 1, false)
//#define DrawAltString(x, y, s)			Draw_String((x), (y), (s), 7, 1, true)
//#define DrawColorString(x, y, s, c, a)	Draw_String((x), (y), (s), (c), (a), false)
//#define DrawString2(x, y, s, a)			DrawCString((x), (y), (s), (a), (cl_textcolors->integer))
#else
void DrawString (int x, int y, const char *s);
void DrawAltString (int x, int y, const char *s);	// toggle high bit
void DrawString2 (int x, int y, const short *s, float alpha);
#define Draw_String(x, y, s, c, a, alt) ((alt) ? DrawAltString((x), (y), (s)) : DrawString((x), (y), (s)))
#define DrawColorString(x, y, s, c, a)	(((c)&2) ? DrawAltString((x), (y), (s)) : DrawString((x), (y), (s)))
#endif

qboolean	CL_CheckOrDownloadFile (const char *filename);

void CL_AddNetgraph (void);

//ROGUE
struct cl_sustain_t
{
	int			id;
	int			type;
	int			endtime;
	int			nextthink;
	int			thinkinterval;
	vec3_t		org;
	vec3_t		dir;
	int			color;
	int			count;
	int			magnitude;
	void		(*think)(cl_sustain_t *self);
};

#define MAX_SUSTAINS		32
void CL_ParticleSteamEffect2(cl_sustain_t *self);

void CL_TeleporterParticles (const entity_state_t *ent);
void CL_ParticleEffect (const vec3_t org, const vec3_t dir, int color, int count);
void CL_ParticleEffect2 (const vec3_t org, const vec3_t dir, int color, int count);

// RAFAEL
void CL_ParticleEffect3 (const vec3_t org, const vec3_t dir, int color, int count);


//=================================================

// ========
// PGM
struct cparticle_t
{
	struct cparticle_t	*next;

	float		time;

	vec3_t		org;
	vec3_t		vel;
	vec3_t		accel;
	int			color;
	//float		color;
	//float		colorvel;
	float		alpha;
	float		alphavel;
};


#define	PARTICLE_GRAVITY	40
#define BLASTER_PARTICLE_COLOR		0xe0
// PMM
#define INSTANT_PARTICLE	-10000.0
// PGM
// ========

void CL_ClearEffects (void);
void CL_ClearTEnts (void);
void CL_BlasterTrail2 (const vec3_t start, const vec3_t end);
void CL_QuadTrail (const vec3_t start, const vec3_t end);
void CL_RailTrail2 (const vec3_t start, const vec3_t end);
void CL_BubbleTrail2 (const vec3_t start, const vec3_t end);
void CL_FlagTrail2 (const vec3_t start, const vec3_t end, float color);

// RAFAEL
void CL_IonripperTrail (const vec3_t start, const vec3_t end);

// ========
// PGM
void CL_BlasterParticles2 (const vec3_t org, const vec3_t dir, unsigned int color);
void CL_BlasterTrail3 (const vec3_t start, const vec3_t end);
void CL_DebugTrail (const vec3_t start, const vec3_t end);
void CL_SmokeTrail (const vec3_t start, const vec3_t end, int colorStart, int colorRun, int spacing);
void CL_Flashlight (int ent, const vec3_t pos);
void CL_ForceWall (const vec3_t start, const vec3_t end, int color);
//void CL_FlameEffects (centity_t *ent, vec3_t origin);
void CL_GenericParticleEffect (const vec3_t org, const vec3_t dir, int color, int count, int numcolors, int dirspread, float alphavel);
void CL_BubbleTrail2 (const vec3_t start, const vec3_t end, int dist);
void CL_Heatbeam (const vec3_t start, const vec3_t end);
void CL_ParticleSteamEffect (const vec3_t org, const vec3_t dir, int color, int count, int magnitude);
void CL_TrackerTrail (const vec3_t start, const vec3_t end, int particleColor);
void CL_Tracker_Explode(const vec3_t origin);
void CL_TagTrail (const vec3_t start, const vec3_t end, int color);
void CL_ColorFlash (const vec3_t pos, int ent, int intensity, float r, float g, float b);
void CL_Tracker_Shell(const vec3_t origin);
void CL_MonsterPlasma_Shell(const vec3_t origin);
void CL_ColorExplosionParticles (const vec3_t org, int color, int run);
void CL_ParticleSmokeEffect (const vec3_t org, const vec3_t dir, int color, int count, int magnitude);
void CL_Widowbeamout (cl_sustain_t *self);
void CL_Nukeblast (cl_sustain_t *self);
void CL_WidowSplash (const vec3_t org);
// PGM
// ========

int CL_ParseEntityBits (sizebuf_t *msg, unsigned int *bits);
void CL_ParseDelta (sizebuf_t *msg, const entity_state_t *from, entity_state_t *to, int number, int bits);
void CL_ParseFrame (sizebuf_t *msg, int extrabits);

void CL_ParseTEnt (sizebuf_t *msg);
void CL_ParseMuzzleFlash (sizebuf_t *msg);
void CL_ParseMuzzleFlash2 (sizebuf_t *msg);
void SmokeAndFlash(vec3_t origin);

void CL_SetLightstyle (int index);

void CL_RunParticles (void);
void CL_RunDLights (void);
void CL_RunLightStyles (void);

void CL_AddEntities (void);
void CL_AddDLights (void);
void CL_AddTEnts (void);
void CL_AddLightStyles (void);

//=================================================

void CL_PrepRefresh (void);
void CL_RegisterSounds (void);

void CL_Quit_f (void);

void IN_Accumulate (void);

//void CL_ParseLayout (void);


//
// cl_main
//

void CL_Init2 (void);

void CL_FixUpGender(void);
void CL_Disconnect (void);
void CL_Disconnect_f (void);
void CL_GetChallengePacket (void);
void CL_PingServers_f (void);
void CL_Snd_Restart_f (void);
void CL_RequestNextDownload (void);

void CL_FixCvarCheats (void);
void CL_RestartFilesystem (qboolean execAutoexec);

//
// cl_input
//
struct kbutton_t
{
	int			down[2];		// key nums holding it down
	unsigned	downtime;		// msec timestamp
	unsigned	msec;			// msec down this frame
	int			state;
};

extern 	kbutton_t 	in_strafe;
extern 	kbutton_t 	in_speed;

void CL_InitInput (void);
void CL_SendCmd (void);

void CL_ClearState (void);

void CL_ReadPackets (void);

void IN_CenterView (void);

const char *Key_KeynumToString (int keynum);

//
// cl_demo.c
//
void CL_WriteDemoMessageFull (sizebuf_t *msg);
void CL_WriteDemoMessage (byte *buff, int len, qboolean forceFlush);

void CL_Stop_f (void);
void CL_Record_f (void);
void CL_InitDemos(void);
void CL_StartAutoRecord(void);
void CL_StopAutoRecord (void);

//
// console.c
//
#if 0
void Con_CheckResize (void);
//void Con_Init (void);
void Con_DrawConsole (float frac, qboolean ingame);
//void Con_Print (const char *txt);
void Con_CenteredPrint (const char *text);
void Con_Clear_f (void);
void Con_DrawNotify (void);
void Con_ClearNotify (void);
void Con_ToggleConsole_f (void);
void Con_SkipNotify ( qboolean skip );
#endif
//
// cl_parse.c
//
extern	const char *svc_strings[256];

void CL_ParseServerMessage (sizebuf_t *msg);
void CL_LoadClientinfo (clientInfo_t *ci, char *s);
void SHOWNET(sizebuf_t *msg, const char *s);
void CL_ParseClientinfo (int player);
void CL_Download_f (void);

//
// cl_view.c
//

void V_Init (void);
void V_RenderView( float stereo_separation );
void V_AddEntity (const renderEntity_t *ent);
void V_AddParticle (const particle2_t *p);
void V_AddLightStyle (int style, const vec3_t value);

// cl_tent.c
void CL_RegisterTEntSounds (void);
void CL_RegisterTEntModels (void);
void CL_SmokeAndFlash(const vec3_t origin);


// cl_pred.c
void CL_InitPrediction (void);
void CL_PredictMove (void);
void CL_CheckPredictionError2 (void);
trace_t CL_PMTrace2 (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end);
int CL_PMpointcontents2 (vec3_t point);

// cl_fx.c
cdlight_t *CL_AllocDlight (int key);
void CL_BigTeleportParticles (const vec3_t org);
void CL_RocketTrail (const vec3_t start, const vec3_t end, centity_t *old);
void CL_DiminishingTrail (const vec3_t start, const vec3_t end, centity_t *old, int flags);
void CL_FlyEffect (centity_t *ent, const vec3_t origin);
void CL_BfgParticles (const renderEntity_t *ent);
void CL_AddParticles2 (void);
void CL_EntityEvent (const entity_state_t *ent);
// RAFAEL
void CL_TrapParticles (renderEntity_t *ent);

#define MAX_LOCAL_SERVERS 9
// menus
void M_Init (void);
//void M_Keydown (int key);
void M_Keydown (int key, qboolean down);
void M_MouseMove( int mx, int my );
void M_Draw (void);
void M_Menu_Main_f (void);
void M_ForceMenuOff (void);
void M_AddToServerList (const serverStatus_t *status);

// cl_inv.c
void CL_ParseInventory (sizebuf_t *msg);
void CL_DrawInventory (void);

// cl_pred.c
void CL_PredictMovement (void);

#if id386
void x86_TimerStart( void );
void x86_TimerStop( void );
void x86_TimerInit( unsigned long smallest, unsigned longest );
unsigned long *x86_TimerGetHistogram( void );
#endif

// cl_locs.c
void CL_FreeLocs(void);
void CL_LoadLoc(void);
void CL_AddViewLocs(void);
void CL_InitLocs(void);

//AVI EXPORT
#ifdef AVI_EXPORT
#include "avi_export.h"
#endif

#include "mp3.h"

#define Q_ColorIndex(c)		(((c) - '0') & Q_COLOR_MASK)

void		CL_DrawChar (float x, float y, float size, int ch, const color_t color, fontSet_t *font, int flags);
void		CL_DrawString (float x, float y, float size, const char *string, const color_t color, fontSet_t *font, int flags);
void		CL_FillRect (float x, float y, float w, float h, const color_t color);
void		CL_DrawPic (float x, float y, float w, float h, const color_t color, struct material_t *material);

void		CL_Draw2D ();

void	    CL_DrawConChar (int x, int y, int ch, const color_t color);
void		CL_DrawConString (int x, int y, const char *string, const color_t color);


void		CL_ClearDynamicLights (void);
void		CL_AddDynamicLights (void);
void		CL_DynamicLight (const vec3_t org, float intensity, float r, float g, float b, bool fade, int duration);

void		CL_ParsePlayerMuzzleFlash (sizebuf_t *msg);
void		CL_ParseMonsterMuzzleFlash (sizebuf_t *msg);

void CL_Loading ();
void		CL_LoadClientInfo (clientInfo_t *ci, const char *string);
void		CL_LoadGameMedia ();
void		CL_LoadLocalMedia ();

void CL_DrawLoading ();